package com.framework.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.ter.TerInfoDetailDao;
import com.framework.entity.sys.SysDict;
import com.framework.entity.ter.TerInfoDetail;
import com.framework.service.DictService;
import com.framework.service.TerInfoDetailService;
import com.framework.utils.string.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author wq
 */
@Service("terInfoDetailService")
@Slf4j
public class TerInfoDetailServiceImpl extends ServiceImpl<TerInfoDetailDao, TerInfoDetail> implements TerInfoDetailService {


    @Autowired
    private DictService dictService;

    @Override
    public void addFlowDetail(Long infoId, String template, Integer infoType, String... strs) {

        if (template.contains("#{") && strs.length > 0) {
            String replaceTemp;
            for (int i = 0; i < strs.length; i++) {
                String str = strs[i];
                replaceTemp = "#{" + (i + 1) + "}";
                template=template.replace(replaceTemp, str);
            }
        }

        String detailId = StringUtil.getDateString(4);

        detailId = verifyDetailId(detailId);

        TerInfoDetail detail = new TerInfoDetail();

        detail.setContent(template);
        detail.setCreateTime(new Date());
        detail.setInfoId(infoId);
        detail.setInfoType(infoType);
        detail.setDetailId(detailId);
        insert(detail);


    }

    @Override
    public void addDistribuDetail(Long infoId, Integer infoType, String disName, String acceptName, Integer isOnline, BigDecimal reapamount, String reapName) {
        TerInfoDetail detail = selectOne(new EntityWrapper<TerInfoDetail>().eq("info_id", infoId).eq("info_type", infoType));
        if(detail==null){
            SysDict detailType = dictService.querySysDict(infoType.toString(), "sn_detail_type");

            if (detailType == null) {
                log.info("未查询到终端记录信息模板，类型为" + infoType + ",infoId:" + infoId + "未添加");
            }
            String template = detailType.getValue();
            String str;
            if (isOnline == 1) {
                str = "线上普通活动返现设备,返现金额为" + reapamount + "元,返现归属" + reapName;
            } else {
                str = "线下设备";
            }

            String[] values = new String[]{disName, acceptName, acceptName, str};
            addFlowDetail(infoId, template, infoType, values);
        }

    }

    @Override
    public void addReportDetail(Long infoId, Integer infoType, String reportName, String ownerName) {
        SysDict detailType = dictService.querySysDict(infoType.toString(), "sn_detail_type");

        if (detailType == null) {
            log.info("未查询到终端记录信息模板，类型为" + infoType + ",infoId:" + infoId + "未添加");
        }
        String template = detailType.getValue();

        String[] values = new String[]{reportName, ownerName};

        addFlowDetail(infoId, template, infoType, values);
    }

    @Override
    public void addDisReportDetail(Long infoId, Integer infoType, String ownerName) {
        SysDict detailType = dictService.querySysDict(infoType.toString(), "sn_detail_type");

        if (detailType == null) {
            log.info("未查询到终端记录信息模板，类型为" + infoType + ",infoId:" + infoId + "未添加");
        }
        String template = detailType.getValue();

        addFlowDetail(infoId, template, infoType, ownerName==null?"无归属":ownerName);
    }

    @Override
    public void addNoParamDetail(Long infoId, Integer infoType) {
        TerInfoDetail detail = selectOne(new EntityWrapper<TerInfoDetail>().eq("info_id", infoId).eq("info_type", infoType));
        if (detail == null) {
            SysDict detailType = dictService.querySysDict(infoType.toString(), "sn_detail_type");

            if (detailType == null) {
                log.info("未查询到终端记录信息模板，类型为" + infoType + ",infoId:" + infoId + "未添加");
            }
            String template = detailType.getValue();
            addFlowDetail(infoId, template, infoType);
        }
    }

    @Override
    public void addActivationDetail(Long infoId, Integer infoType, BigDecimal reapAmount) {

        SysDict detailType = dictService.querySysDict(infoType.toString(), "sn_detail_type");

        if (detailType == null) {
            log.info("未查询到终端记录信息模板，类型为" + infoType + ",infoId:" + infoId + "未添加");
        }
        String template = detailType.getValue();

        addFlowDetail(infoId, template, infoType,reapAmount==null?"0":reapAmount.toString());


    }

    String verifyDetailId(String detailId) {
        TerInfoDetail detail = baseMapper.selectById(detailId);
        if (detail != null) {
            detailId = StringUtil.getDateString(4);
            verifyDetailId(detailId);
        }
        return detailId;
    }

}
